<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\response\Json;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use app\common\model\Assets;

class UserAuth extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\UserAuth';

    //关联模型
    protected $hasOne = ['user'];

    protected $append = ['status_name'];

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'nickname', 'title' => '昵称', 'templet' => '<div>{{d.user.nickname}}</div>'])
            ->addRow(['field' => 'code', 'title' => '编号', 'templet' => '<div>{{d.user.code}}</div>'])
            ->addRow(['field' => 'mobile', 'title' => '手机号', 'templet' => '<div>{{d.user.mobile}}</div>'])
            ->addRow(['field' => 'real_name', 'title' => '真实姓名'])
            ->addRow(['field' => 'id_card', 'title' => '身份证号'])
            //->addRow(['field' => 'img_front', 'title' => '身份证正面图'])
            //->addRow(['field' => 'img_back', 'title' => '身份证正反面图'])
            ->addRow(['field' => 'create_time', 'title' => '时间', 'width' => 150])
            ->addRow(['field' => 'status_name', 'title' => '状态'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['title' => '查看', 'height' => 900]])
            ->addTopButtons(['batchAudit' => [
                'title' => '批量审核', 'icon' => 'layui-icon-util',
                'url' => url('UserAuth/batch')->build(), 'onclick' => 'buttons.batchAudit($(this));'], 'export'])
            ->setSearch($this->setSearch())
            ->addSwitchTool([['name' => 'is_lock', 'pk' => 'id', 'text' => '锁定|正常']])
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']);
    }

    public function edit($name = '', $id = '', $tpl = 'user_auth/edit', $info = '')
    {
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

    public function batch()
    {
        return \app\common\model\UserAuth::batch($this->request->param('ids'), $this->admin);
    }
}
